package com.gxa.controller.community;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.common.utils.R;
import com.gxa.common.utils.Result;
import com.gxa.entity.community.Community;
import com.gxa.entity.discrepancy.Device;
import com.gxa.mapper.community.CommunityMapper;
import com.gxa.service.community.CommunityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static com.gxa.common.utils.R.error;
import static com.gxa.common.utils.R.ok;

/**
 * @ClassName CommunityController
 * @Description
 * @Author Mr.YUAN
 * @Date 2022/10/11 18:20
 */
@Api(tags = "社区管理接口")
@RestController
@RequestMapping("/community")
public class CommunityController {

   @Autowired
   private CommunityService communityService;

    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation(value = "社区列表查询接口")
    @PostMapping("/list")
    public R queryAll(@RequestBody Community community, Integer pageNum,Integer pageSize){
//        List<Community> communities;
//        PageInfo<Community> pageInfo;
//        try{
//            if (pageNum==0 && pageSize == 0){
//                Page<Community> page = PageHelper.startPage(0);
//                communities = communityService.query();
//                pageInfo = new PageInfo<>();
//            }else {
//                Page<Community> page = PageHelper.startPage(pageNum, pageSize);
//                communities = communityService.query();
//                pageInfo = new PageInfo<>();
//              }
//            }catch (Exception e){
//                e.printStackTrace();
//                return error("error");
//            }
        Map<String, Object> map;
        PageInfo<Community> pageInfo = null;
        try{
             map = communityService.queryAll(community,pageNum, pageSize);
             pageInfo = (PageInfo<Community>) map.get("pageInfo");
        }catch (Exception e){
            e.printStackTrace();
            return error("error");
        }

        return ok("successs").put("pageInfo",pageInfo);
    }

    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation(value = "新增社区列表接口")
    @PostMapping("/add")
    public R add(@RequestBody Community community){
        try{
            communityService.add(community);
        }catch (Exception e){
            e.printStackTrace();
            return error("add error");
        }
        return ok("add success");
    }

    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation(value = "删除社区接口")
    @DeleteMapping("/del/{id}")
    public R del(@PathVariable("id") Integer id){
        try{
            communityService.deleteById(id);
        }catch (Exception e){
            e.printStackTrace();
            return error("del error");
        }
        return ok("del success");
    }

    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation(value = "根据小区编号或者社区名称查询社区接口")
    @GetMapping("/queryByNumName")
    public R queryByIdOrName(String comNum,String comName){
        System.out.println("小区名称："+comName);
        System.out.println("小区编号："+comNum);
        List<Community> communities;
        try{
           communities = communityService.queryByIdOrName(comNum, comName);
        }catch (Exception e){
            e.printStackTrace();
            return error("query error");
        }
        return R.ok("query success").put("communities",communities);
    }

    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation(value = "编辑社区接口")
    @PutMapping("/edit")
    public R edit(@RequestBody Community community){
        try{
            communityService.update(community);
        }catch (Exception e){
            e.printStackTrace();
            return error("edit error");
        }
        return ok("edit success");
    }

    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation(value = "异步请求判断社区编号是否重复的接口")
    @GetMapping("/judgeOne")
    public Result judgeOne(String communityNum){
        List<Community> communityList = communityService.queryByNum(communityNum);
        if (communityList.size() != 0){
            return Result.failed("编码重复了，请重新输入");
        }else {
            return Result.success();
        }
    }

}